package io.bdmc.modules.sys.controller;

import java.time.LocalDateTime;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import io.bdmc.common.annotation.SysLogger;
import io.bdmc.common.utils.MyPage;
import io.bdmc.common.utils.R;
import io.bdmc.modules.sys.entity.SysDictData;
import io.bdmc.modules.sys.entity.SysDictType;
import io.bdmc.modules.sys.model.SysDictTypeParam;
import io.bdmc.modules.sys.service.ISysDictDataService;
import io.bdmc.modules.sys.service.ISysDictTypeService;

/**
 * <p>
 * 字典类型表 前端控制器
 * </p>
 *
 * @author 黄露
 * @since 2021-04-28
 */

// "字典类型表", description = "字典类型表")
@RestController
@RequestMapping("/sys/dictType")
public class SysDictTypeController extends AbstractController {

	@Autowired
	ISysDictTypeService _sysdicttypeSvs;
	@Autowired
	ISysDictDataService _dictDataService;

	@GetMapping("/list")
	// "分页查询字典类型表", notes = "分页查询字典类型表")
	@RequiresPermissions("sys:dictType:list")
	public R list(SysDictTypeParam params) {
		MyPage<SysDictType> myPage = new MyPage<SysDictType>(params.getCurrent(), params.getSize());
		myPage = _sysdicttypeSvs.mySelectPage(myPage, params);
		return R.ok().putPageData(myPage.getRecords(), myPage.getTotal());

	}

	@SysLogger("新增字典类型表")
	@PostMapping("/add")
	// "新增字典类型表", notes = "新增字典类型表")
	@RequiresPermissions("sys:dictType:add")
	public R add(@RequestBody SysDictType sysdicttype) {
		sysdicttype.setCreateBy(getUser().getUsername());
		sysdicttype.setCreateTime(LocalDateTime.now());
		Boolean b = sysdicttype.insert();
		return R.trueOrFalse(b);
	}

	@SysLogger("修改字典类型表")
	@PostMapping("/update")
	// "修改字典类型表", notes = "修改字典类型表")
	@RequiresPermissions("sys:dictType:update")
	public R update(@RequestBody SysDictType sysdicttype) {
		Boolean b = _sysdicttypeSvs.updateDictType(sysdicttype);
		return R.trueOrFalse(b);
	}

	@SysLogger("删除字典类型表")
	@DeleteMapping("/delete/{dictId}")
	// "删除字典类型表", notes = "删除字典类型表")
	@RequiresPermissions("sys:dictType:delete")
	public R delete(@PathVariable Integer dictId) {
		if (dictId > 0) {
			SysDictType dt = _sysdicttypeSvs.getById(dictId);
			_dictDataService.remove(new QueryWrapper<SysDictData>().eq("dict_type", dt.getDictType()));
			_sysdicttypeSvs.removeById(dictId);
		}
		return R.ok();
	}
}
